dhcpv4: lazy store statefiles
authorDavid Härdeman <[email protected]>
Fri, 10 Oct 2025 17:55:36 +0000 (19:55 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Wed, 5 Nov 2025 14:58:40 +0000 (15:58 +0100)
commit7d1b081dcb1b4616307e871e760d6a080d843856
tree50da1327ebcc4c3fd33e80116e2361ac824ea3ee
parentd44af6dd8f4e1dd0d858ae19419057ab4f319310
dhcpv4: lazy store statefiles

Currently, the dhcpv4 server saves the statefile on every change and
also calls the leasetrigger script every time the statefile is updated
(which triggers a dnsmasq reload).

In addition, odhcpd wakes up every second (see dhcpv4_valid_until_cb())
to go through all existing leases to see if any lease has expired.

With this change, the wakeups are reduced to every 5 seconds, and the
statefile is only written (if necessary) during that wakeup.

Before this change (without any leasetrigger, test on my laptop, not a
real OpenWrt device):

$ time sudo ./build/dhcpdig -4 benchmark foo-client

Thread[**]: req 10000101/0 rel 10000101/0 rep 10000101/0

real 55m29.406s
user 0m0.005s
sys 0m0.010s

(This is a simple benchmark tool I wrote, it runs 10 threads which
divide the DHVPv4 pool into 10 chunks and then performs 1 million random
addr req/release per thread in a loop).

After this change:

$ time sudo ./build/dhcpdig -4 benchmark foo-client

Thread[**]: req 10000101/0 rel 10000101/0 rep 10000101/0

real 1m48.123s
user 0m0.005s
sys 0m0.005s

A 3082% speedup.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/298
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/dhcpv4.c
src/odhcpd.h